package com.ruoyi.system.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.ruoyi.system.domain.AddForm;
import com.ruoyi.system.util.MsgUtil;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.*;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class PublicConsumer {

    @Autowired
    MsgUtil msgUtil;
    @Autowired
    RestHighLevelClient esRestClient;

    @RabbitListener(queues = "PublicQueueA")
    public void consumerQueueA(Message message, Channel channel) throws IOException {
        System.err.println("收到消息A:==============="+message.getBody()+"=================");


        try{
            IndexRequest roominfo = new IndexRequest("roominfo");
            roominfo.source(new String(message.getBody()),XContentType.JSON);
            esRestClient.index(roominfo,RequestOptions.DEFAULT);
            //消息确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            // // 丢弃该消息
            e.printStackTrace();
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
        }

    }

    @RabbitListener(queues = "PublicQueueB")
    public void consumerQueueB(Message message, Channel channel) throws IOException {
        System.err.println("收到消息B:==============="+message.getBody()+"=================");
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
